;;;; utilities.lisp

(in-package "utilities")

;;; "utilities" goes here. Hacks and glory await!

(defun hash-to-alist (hash-table)
  (let ((result ()))
    (maphash (lambda (key value) (setf result (append (list (cons key value)) result))) hash-table)
    result))

(defun alist-to-hash (list)
  (let ((result (make-hash-table)))
  (loop for x in list
       do (setf (gethash (car x) result) (cdr x)))
  result))

(defun write-object (input &optional (file-name "test.obj"))
  (with-open-file (stream 
		   (data-file-name file-name) :direction :output :if-exists :supersede)
    (prin1 input stream)))

(defun read-object (file-name)
  (with-open-file (stream 
		   (data-file-name file-name) :direction :input)
    (read stream)))

(defun data-file-name (file-name)
  (concatenate 'string *data-store-path* file-name))

(defun get-hash-alist (hash-table hash-key list-key)
  (cdr (assoc list-key (gethash hash-key hash-table))))

(defun get-all-units-of-race (race)
  (let ((read-all-units-stats (read-object "unit-stats.obj")))
    (loop for unit in read-all-units-stats 
       when (eq race (cdr (assoc :RACE (cdr unit))))
       collect (list unit)
	 )))